//
//  HWGameLiveChatProtocol.swift
//  PlayWithProject
//
//  Created by 王昌健 on 2021/10/26.
//

import UIKit

class HWGameLiveChatProtocol: NSObject {
    var chatModelArr:[HWGameLiveChatModel]?
    var gameModel:HWVoiceRoomGameModel?
    var downLoadButtonClick:((_:HWVoiceRoomGameModel)->())?
}

extension HWGameLiveChatProtocol:UITableViewDelegate,UITableViewDataSource {
    
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let header = HWGameLiveChatGameHeader.headerOrFooterWithTableView(tableView: tableView)
        header.gameModel = gameModel
        header.downLoadButtonClick = {
            self.downLoadButtonClick?(self.gameModel ?? HWVoiceRoomGameModel())
        }
        return header
    }
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 92*widthScale
    }
    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 0
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return (self.chatModelArr?.count ?? 0) + 1
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let systemCell = HWGameLiveChatSystemCell.cellWithTableView(tableView: tableView)
        let enterRoomCell = HWGameLiveChatEnterRoomCell.cellWithTableView(tableView: tableView)
        let chatCell = HWGameLiveChatCell.cellWithTableView(tableView: tableView)
        if indexPath.row == 0 {
            return systemCell
//        }else if indexPath.row == 1{
//            enterRoomCell.model = chatModelArr?[indexPath.row - 1]
//            return enterRoomCell
        }else{
//            chatCell.model = chatModelArr?[indexPath.row-2]
//            return chatCell
            
            if chatModelArr?[indexPath.row - 1].msgType == HWLiveRoomMsgType.userEnterRoom {
                enterRoomCell.model = chatModelArr?[indexPath.row - 1]
                 return enterRoomCell
             }else {
                 chatCell.model = chatModelArr?[indexPath.row - 1]
                 return chatCell
             }
        }
    }
    
    
}
